package com.glodon.paas.account.security.cas.ticket.registry;

import com.glodon.paas.account.security.cas.ticket.support.RedisExpirationPolicy;
import org.jasig.cas.authentication.ImmutableAuthentication;
import org.jasig.cas.authentication.principal.SimplePrincipal;
import org.jasig.cas.ticket.Ticket;
import org.jasig.cas.ticket.TicketGrantingTicketImpl;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.*;

/**
 * @author Don Li
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-config-test.xml")
public class RedisTicketRegistryIT {
    @Autowired
    @Qualifier("tgtRegistry")
    RedisTicketRegistry tgtRegistry;

    @Autowired
    JedisPool pool;

    @Autowired
    @Qualifier("tgtExpirationPolicy")
    RedisExpirationPolicy tgtExpirationPolicy;

    @Test
    public void test_add_ticket() {
        Ticket ticket = new TicketGrantingTicketImpl("test1", new ImmutableAuthentication(new SimplePrincipal("user1")), tgtExpirationPolicy);
        tgtRegistry.addTicket(ticket);
        Ticket t = tgtRegistry.getTicket(ticket.getId());
        assertNotNull(t);
        assertThat(t.getId(), is("test1"));
        assertThat(t.getCountOfUses(), is(0));
        assertTrue(t instanceof TicketGrantingTicketImpl);
        assertThat(((TicketGrantingTicketImpl) t).getAuthentication().getPrincipal().getId(), is("user1"));
    }

    @Test
    public void test_delete_ticket() {
        Ticket ticket = new TicketGrantingTicketImpl("test1", new ImmutableAuthentication(new SimplePrincipal("user1")), tgtExpirationPolicy);
        tgtRegistry.addTicket(ticket);
        tgtRegistry.deleteTicket(ticket.getId());
        assertNull(tgtRegistry.getTicket(ticket.getId()));
    }

    @Test
    public void test_get_tickets() {
        tgtRegistry.addTicket(new TicketGrantingTicketImpl("test1", new ImmutableAuthentication(new SimplePrincipal("user1")), tgtExpirationPolicy));
        tgtRegistry.addTicket(new TicketGrantingTicketImpl("test2", new ImmutableAuthentication(new SimplePrincipal("user2")), tgtExpirationPolicy));
        tgtRegistry.addTicket(new TicketGrantingTicketImpl("test3", new ImmutableAuthentication(new SimplePrincipal("user3")), tgtExpirationPolicy));
        assertThat(tgtRegistry.getTickets().size(), not(0));
    }

    @After
    public void tearDown() {
        final Jedis jedis = pool.getResource();
        for (String key : jedis.keys(RedisTicketRegistry.DEFAULT_TGT_PREFIX + "test*")) {
            jedis.del(key);
        }
        pool.returnResource(jedis);
    }
}
